package com.shuoyue.ycgk.ui.course.adapter;

import android.graphics.Color;
import android.text.Html;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.entity.TimerTable;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.ArrayList;

public class TimerTableSimpleAdapter extends AppBaseQuickAdapter<TimerTable> {
    String fontSort = "";
    boolean isShowTrySee = true;

    public TimerTableSimpleAdapter(@Nullable ArrayList<TimerTable> data, ClickCallBack clickCallBack) {
        this(data, true, clickCallBack, "章");
    }

    public TimerTableSimpleAdapter(@Nullable ArrayList<TimerTable> data,
                                   boolean isShowTrySee,
                                   ClickCallBack clickCallBack,

                                   String fontSort) {
        super(R.layout.item_timetable_simple, data);
        this.clickCallBack = clickCallBack;
        this.fontSort = fontSort;
        this.isShowTrySee = isShowTrySee;

    }

    @Override
    protected void convert(BaseViewHolder helper, TimerTable item) {
        helper.getView(R.id.lay_title).setOnClickListener(v -> whenclick(item));
//        helper.setText(R.id.tv_title, item.getName() + (item.getTrySeeTime() == 0 || !isShowTrySee ? "" : "【试看】"));// + (item.getTrySeeTime() < 60 ? "" : "(试看" + (item.getTrySeeTime() / 60) + "分)"));

        helper.setText(R.id.tv_title, Html.fromHtml(item.getName() + (item.getTrySeeTime() != 0 &&  isShowTrySee ?
                "<font color='#FF4A26'> 【试看】</font> " : "")));// + (item.getTrySeeTime() < 60 ? "" : "(试看" + (item.getTrySeeTime() / 60) + "分)"));

        helper.setGone(R.id.time_count, item.getDuration() != 0);
        helper.setText(R.id.time_count, "时长:" + item.getDuration() + "分钟");
        helper.setGone(R.id.sort, fontSort != null);
        helper.setText(R.id.sort, fontSort == null ? "" : ("第" + (helper.getAdapterPosition() + 1) + fontSort + "    "));
        helper.setBackgroundColor(R.id.recycler_view, Color.parseColor(fontSort == null ? "#ffffff" : "#f4f4f4"));

        RecyclerView recyclerView = helper.getView(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        if (recyclerView.getTag() == null
//                &&item.getNodes()!=null&&item.getNodes().size()>0
        ) {
            recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 4), false));
            recyclerView.setTag("tag");
        }
        if (item.getNodes() != null && item.getNodes().size() > 0) {
            TimerTableSimpleAdapter timerTableAdapter;
            if (fontSort == null) {
                timerTableAdapter = new TimerTableSimpleAdapter(item.getNodes(), isShowTrySee, clickCallBack, null);
            } else if (fontSort.equals("章")) {
                timerTableAdapter = new TimerTableSimpleAdapter(item.getNodes(), isShowTrySee, clickCallBack, "节");
            } else if (fontSort.equals("节")) {
                timerTableAdapter = new TimerTableSimpleAdapter(item.getNodes(), isShowTrySee, clickCallBack, null);
            } else {
                timerTableAdapter = new TimerTableSimpleAdapter(item.getNodes(), isShowTrySee, clickCallBack, null);
            }


            recyclerView.setAdapter(timerTableAdapter);
        }
        //处理图标和展开状态
        if (item.getNodes() != null && item.getNodes().size() > 0) {
//            helper.setBackgroundColor(R.id.lay_title, Color.parseColor("#ffffff"));
            helper.setVisible(R.id.icon, true);
            helper.setImageResource(R.id.icon, item.isSelect() ? R.mipmap.icon_arrow_up : R.mipmap.icon_arrow_down);
            helper.setGone(R.id.recycler_view, item.isSelect());
        } else {
//            helper.setBackgroundColor(R.id.lay_title, Color.parseColor("#f4f4f4"));
            helper.setGone(R.id.recycler_view, false);
            helper.setVisible(R.id.icon, true);
            helper.setImageResource(R.id.icon, item.isSelect() ? R.mipmap.play_blue : R.mipmap.play_gray);
        }
    }

    /**
     * 处理点击事件
     *
     * @param timertabler
     */
    private void whenclick(TimerTable timertabler) {
        if (timertabler.getNodes() != null && timertabler.getNodes().size() > 0) {
            //有子元素的,内部处理展开即可
            timertabler.setSelect(!timertabler.isSelect());
            notifyDataSetChanged();
        } else if (clickCallBack != null) {
            clickCallBack.play(timertabler);
        }
    }

    ClickCallBack clickCallBack;


    public interface ClickCallBack {
        void play(TimerTable timerTable);
    }

    @Override
    public ArrayList<TimerTable> getData() {
        return (ArrayList<TimerTable>) super.getData();
    }
}
